package com.yicong.ants.ui.circle.ad;

import android.util.Pair;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.ui.adapter.DataBindQuickAdapter;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.circle.AdItem;
import com.yicong.ants.databinding.CommonRecylerBinding;
import com.yicong.ants.manager.AdapterHelper;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * 我的广告
 */
public class AdManagerActivity extends BaseTitleBarActivity<CommonRecylerBinding> {
    StatefulBindQuickAdapter<AdItem> mAdapter;

    @Override
    protected int getLayout() {
        return R.layout.common_recyler;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("我的广告");
        initAdapter();
        onLoadData();
        mDataBind.swipeRefresh.setOnRefreshListener(this::onLoadData);
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (event.getCode()) {
            case Constants.Event.Expect_Refresh_Manager_Ad:
                onLoadData();
                break;
        }
    }

    private void initAdapter() {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new StatefulBindQuickAdapter<AdItem>(R.layout.circle_ad_manager_item, null) {
            @Override
            public void loadPageData(int page) {
                loadPage(page);
            }

            @Override
            protected void convert(DataBindQuickAdapter.DataBindViewHolder helper, AdItem item) {
                helper.getBinding().setVariable(BR.bean, item);
                helper.getBinding().setVariable(BR.click, (View.OnClickListener) v -> {
                    switch (v.getId()) {
                        case R.id.stop:
                            UiHelper.showCancelDialog(mContext, "是否确定停止投放？",
                                Pair.create("确定", (dialog, which) -> {
                                    dialog.dismiss();
                                    showProgress();
                                    Map<String, String> map = new HashMap<>();
                                    map.put("ad_id", item.getId());
                                    addSubscribe(RetrofitHelper.getApis().AdOffLine(map)
                                        .compose(RxHelper.toMain())
                                        .subscribe(respBean -> {
                                            hideProgress();
                                            if (respBean.isCodeFail()) {
                                                showToast(respBean.getMsg());
                                                return;
                                            }
                                            UiHelper.showConfirmDialog(mContext, respBean.getMsg(), null);
                                            onLoadData();
                                        }, RxHelper.getHideProgressConsumer(AdManagerActivity.this)));
                                }),
                                Pair.create("取消", (dialog, which) -> dialog.dismiss()));
                            break;
                        case R.id.view:
                            Dialogs.showCircleAd(mContext, item, null);
                            break;
                        case R.id.edit:
                            Router.turnTo(mContext, AdEditActivity.class)
                                .putExtra(Constants.Extra.JSON, JsonHelper.toJson(item))
                                .start();
                            break;
                    }
                });
            }
        };
        mAdapter.bindToRecyclerView(mDataBind.recycler);
        mDataBind.recycler.addItemDecoration(AdapterHelper.getVerticalDividerItemDecoration(16, 12));
    }

    @Override
    protected void onLoadData() {
        loadPage(1);
    }

    private void loadPage(final int page) {
        final Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));

        if (page == 1) {
            mAdapter.setViewState(MultiStateView.VIEW_STATE_LOADING);
        }
        addSubscribe(RetrofitHelper.getApis().adManager(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                mDataBind.swipeRefresh.setRefreshing(false);
                AdapterHelper.solvePageBean(mDataBind.recycler, page, respBean);
            }, RxHelper.getSwitchableErrorConsumer(mAdapter)));
    }
}
